<?php
class NewsController extends Controller
{
    function __construct()
    {
        $this->loadModel('news');
        $this->loadLibrary('Utility');
        $this->loadLibrary('Session');
        $this->loadLibrary('recaptcha');
    }

    public function show($id = NULL)
    {
        $data = NULL;
        $this->loadModel('category',TRUE);
        $this->loadModel('comment');
        $this->loadModel('ad',TRUE);
        
        if (is_null($id))
        {
            Utility::error("Có lỗi xảy ra !", 'index');
            exit();
        }
        $rs = $this->models['news']->getNews($id);

        if($rs->getSize() == 0)
        {
            Utility::error("Bài viết không tồn tại");
            exit();
        }
        else
        {
            if(isset($_POST['author_name']))
            {

                $result = $this->models['comment']->handleAdd();
                if($result['onError'])
                {
                    $data['errorMessage'] = $result['errorMessage'];
                }
                else
                    $data['cmt_ok'] = $result['cmt_message'];
            }

            if(isset($_POST['deletedCmt']))
            {
                
                $session = Session::getInstance();
                if($session->isExist('id'))
                {
                    $this->models['comment']->remove($_POST['deletedCmt']);
                }
                else
                {
                    Utility::error("Bạn không đủ quyền hạn để thực hiện chức năng này");
                    exit();
                }
            }

            $cat_list = array();
            $this->models['category']->getCategories(0, $cat_list);

            $data['cat_list'] = $cat_list;
            $data['news'] = $rs->getRow(0);
            $data['ad_list'] = $this->models['ad']->getAd();
            $rs2 = $this->models['comment']->getCommentList($id);
            if($rs2->getSize() > 0)
            {
                $data['cmt_list'] = $rs2->getAll();
            }
            $view = $this->loadView('news');
            $view->setBlockData($data, 'adv');
            $view->setBlockData($data, 'cat_list');
            $view->setBlockData($data);
            $view->setBlockData($data['news']->title,'title');
            $view->render();
        }
    }

    function index($page = 1)
    {
        $view = $this->loadView('index');
        $this->loadModel('category', TRUE);
        $this->loadModel('news');
        $data = NULL;
        $cat_list = array();
        $this->models['category']->getCategories(0, $cat_list);

        $data['cat_list'] = $cat_list;

        $view->setBlockData($data, 'cat_list');
        $data['album_items'] = $this->models['news']->getLastestAlbum();
        $data['lastest_news'] = $this->models['news']->getLastestNews($page - 1);//var_dump($data['lastest_news']);exit();
        $view->setBlockData($data);
        $view->setBlockData('Trang chủ', 'title');
        $view->addJs('jquery.tools.min');
        $view->render();
    }
}